import { message } from "antd";
import { useEffect } from "react";
import { useNavigate } from "react-router-dom";

export default function Auth({ children }: any) {
    const navigate = useNavigate();
    const authString = localStorage.getItem('auth');
    const auth = authString ? JSON.parse(authString) : null;

    useEffect(() => {
        if (!auth) {
            message.destroy();
            message.error("您还未登录!");
            navigate("/login", { replace: true });
        } else {
            let nowTime = new Date().getTime();
            if (nowTime - auth.token >= 1000 * 60 * 60 * 36) {
                message.error("登录过期,请重新登录");
                navigate("/login", { replace: true });
            }
        }
    }, []);

    return children
}